﻿// Shader-Based-Image-Processing (SBIP)
// http://code.google.com/p/sbip/
//
// Copyright © Frank Nagl, 2009-2010
// admin@franknagl.de
//
namespace SBIP.Filter
{
    using Microsoft.Xna.Framework.Graphics;
    using System;

    /// <summary>
    /// Threshold binarization.
    /// </summary>
    /// 
    /// <remarks>
    /// <para>
    /// The filter does image binarization using specified threshold value. All pixels (or 
    /// one channel of them) with intensities higher than threshold value are converted to white 
    /// pixels. All other pixels with intensities equal or below threshold value are converted to black pixels.
    /// </para>
    /// 
    /// <para>Sample usage:</para>
    /// <code>
    ///  // 1. Offline rendering
    ///  // create Processor, used as rendering framework
    ///  Processor processor = new Processor();
    ///  // starts Processor
    ///  processor.Begin( image );
    ///  // create Threshold filter
    ///  Threshold filter = new Threshold( );
    ///  processor.Filter = filter;
    ///  // optional: configure filter
    ///  filter.Threshold = 170;
    ///  // apply the filter  
    ///  Bitmap resultImage = processor.RenderToBitmap( );
    ///  Texture2D resultTexture = processor.RenderToTexture( );
    ///  processor.End( );
    ///  
    ///  // 2. Online rendering
    ///  // create any windows control for rendering in
    ///  Form myForm = new Form( );
    ///  // create Processor, used as rendering framework
    ///  Processor processor2 = new Processor( );
    ///  // starts Processor
    ///  processor2.Begin( image, myForm );
    ///  // create Threshold filter
    ///  Threshold filter2 = new Threshold( );
    ///  processor2.Filter = filter2;  
    ///  // apply the filter
    ///  myForm.Show( );
    ///  while ( myForm.Created )
    ///  {
    ///      processor2.Render( );
    ///      Application.DoEvents( );
    ///  }
    ///  processor2.End( );
    /// </code>
    /// 
    /// <para><b>Initial image:</b></para>
    /// <img src="img/shaderbased/sample1.jpg" width="480" height="361" />
    /// <para><b>Result image (Threshold = 170):</b></para>
    /// <img src="img/shaderbased/Threshold.jpg" width="480" height="361" />
    /// </remarks>
    public sealed class Threshold : BaseFilter
    {
        private byte value;
        /// <summary>Threshold value.</summary>
        /// <remarks><para>Default value is set to 100.</para></remarks>
        public byte Value
        {
            get { return value; }
            set 
            {
                if (Effect == null)
                    throw new ArgumentException("Threshold" + InitMsg);

                this.value = value;
                Effect.Parameters["Threshold"].SetValue(value);
            }
        }      
        
        /// <summary>
        /// Initializes a new instance of the <see cref="Threshold"/> class.
        /// </summary>
        public Threshold()
            : base("Threshold") { }

        /// <summary>
        /// Initializes threshold value.
        /// </summary>
        protected override void PostInit()
        {
            Value = 100;
        }
    }
}
